package com.hei.controller;

import com.hei.pojo.Barticle;
import com.hei.pojo.Bkind;
import com.hei.pojo.Blabel;
import com.hei.pojo.PageHelper;
import com.hei.pojo.dto.BarticleDto;
import com.hei.service.BarticleService;
import com.hei.service.BkindService;
import com.hei.service.BlabelService;
import com.hei.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController {
    @Autowired
    private BarticleService barticleService;

    @Autowired
    private BkindService bkindService;

    @Autowired
    private BlabelService blabelService;

    @RequestMapping("/")
    public String toIndex1(){
        return "redirect:/index/1";
    }
    @RequestMapping("/index/{cp}")
    public String toIndex(@PathVariable("cp") int currPage, Model model){
        PageHelper<BarticleDto> barticles = barticleService.queryAllArticle(currPage);
        model.addAttribute("page",barticles);
        return "index";
    }

    @RequestMapping("/side")
    @ResponseBody
    public ResponseMessage querySideBar(){
        List<Bkind> kinds = bkindService.queryUsedKind();
        List<Blabel> labels = blabelService.queryUsedLabel();
        return ResponseMessage.success(200,"成功").addObject("kinds",kinds).addObject("labels",labels);
    }

    @RequestMapping("/kind/{id}/{cp}")
    public String selectByKind(@PathVariable( "cp") int currPage
            ,@PathVariable("id")String kid,Model model){
        PageHelper<BarticleDto> PageHelper = barticleService.queryArticleByKid(currPage, kid);
        model.addAttribute("page",PageHelper);
        model.addAttribute("kind",kid);
        return "index";
    }

    @RequestMapping("/label/{id}/{cp}")
    public String selectByLabel(@PathVariable("cp") int currPage
            ,@PathVariable("id")String lid,Model model){
        PageHelper<BarticleDto> PageHelper = barticleService.queryArticleByLid(currPage, lid);
        model.addAttribute("page",PageHelper);
        model.addAttribute("label",lid);
        return "index";
    }


}
